Skip to content

Stream sampling [merge after chunk loading] - #45

Merged
mads-bertelsen merged 11 commits into
mccode-dev:mainfrom
mads-bertelsen-agentic:stream_sampling
Oct 16, 2026
Merged

mads-bertelsen merged 11 commits into
mccode-dev:mainfrom
mads-bertelsen-agentic:stream_sampling

Conversation

@mads-bertelsen-agentic

Copy link
Copy Markdown
Contributor

Aaron has used a really nice solution for sampling McStas weighted events to unit events while working with simulated data for NMX. He has verbally agreed to me sharing the code by adding it to this package.

His work can be found here: https://github.com/aaronfinke/stream_sampling_mcstas

This implementation support the chunk loading, and should be merged after that pull request is accepted.

Comment thread src/mcstastox/Sampling.py Outdated
class SampledEventData(dict[str, np.ndarray]):
"""Sampled event arrays and the total weight of the input stream."""

def __init__(self, event_data: dict[str, np.ndarray], total_weight: float):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event_data can be broken into multiple keywords or sth more clear?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added doc string to better explain whats going on here! We could make it take arbitrary keyword arguments like a dict() constructor, but it will be more or less the same thing in the end. This class is only constructed as a way to report the data back to the user, so it's not something we expect the user to be able to init ever

Comment thread src/mcstastox/Sampling.py Resolved

@YooSunYoung YooSunYoung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me...! I just had a minor question, and it's not a blocker I think.
Let's merge/release it and then I'll try making a code shelf notebook with it...!

square=True, banana=True, id_overlap=False, id_gap=False, non_zero_id_start=False
):
instr = ms.McStas_instr("test")
instr = ms.McStas_instr(f"test_{os.getpid()}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to make the instrument unique?
Then we can use uuid instead maybe.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since multiple documentation notebooks build that instrument and execute it in parallel, it is to avoid the data files being named the same. In sequence, it is detected and avoided, but not in parallel.

I think this problem can be solved in a nicer way by only making the data folder name funny, not also the instrument name. Would be nice to merge and get it in a later round!

@mads-bertelsen
mads-bertelsen merged commit 66ac649 into mccode-dev:main Oct 16, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants